<extend name="Public:base" />
<block name="content">
    <form method="post" action="{:U('coupon/send')}">
        <table class="table table-striped table-bordered table-hover search-form">
            <thead>
            <th>电子券号：<input name="number" type="text"  value="{:I('number')}" />
                电子券名称：<input name="name" type="text"  value="{:I('name')}" />
                <a class="btn btn-primary radius5 btn-sm" href="javascript:;" id="search" url="{:U('Coupon/manage')}">查询</a>
                <a  style="{$update==1?'':'display: none'}" href="{:U('Coupon/addcoupon')}" class="btn btn-primary radius5 btn-sm">添加电子券</a>
                <input style="{$update==1?'':'display: none'}"  class="btn btn-primary radius5 btn-sm"  id="submitBtn" type="submit" value="发放"  class="btn btn-primary" >
                <a  style="{$delete==1?'':'display: none'}" class="btn btn-danger radius5 btn-sm"  id="Del"/><i class="ace-icon fa">删除</i></a>
            </th>
        </table>

        <div class="row">
            <div class="col-xs-12">
                <div class="table-responsive">
                    <table class="table table-striped table-bordered table-hover" id="mytable">
                        <thead>
                        <tr>
                            <th class="center" style="width: 30px;"><input id="CheckAll" type="checkbox"  style="margin-right: 0"  /></th>
                            <th class="col-sm-1">电子券号</th>
                            <th class="col-sm-1">电子券名称</th>
                            <th class="col-sm-1">面值</th>
                            <th class="col-sm-3">可用范围</th>
                            <th class="col-sm-1">可用条件</th>
                            <th class="col-sm-1">状态</th>
                            <th class="col-sm-1">有效期</th>
                            <th class="col-sm-1">类型</th>
                            <th class="col-sm-1">创建时间</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <volist name="list" id="vo">
                            <tr>
                                <td  class="center"><input name="coupon[]" value="{$vo.volume_id}" type="checkbox" /></td>
                                <td>{$vo.volume_number}</td>
                                <td>{$vo.volume_name}</td>
                                <td>¥{$vo.total}</td>
                                <td>{$vo.category}</td>
                                <td>订单满{$vo.min}¥可用</td>
                                <td>
                                    <?php
								switch($vo['status']){
									case '1':
									echo '<span class="green bold">已生效</span>';
                                    break;
                                    case '0':
                                    echo '<span class="red bold">已失效</span>';
                                    break;
                                    }
                                    ?>
                                </td>
                                <td>开始时间：{$vo.start_time} 结束时间：{$vo.end_time}</td>
                                <td><?php
								switch($vo['type']){
									case '1':
									echo '<span class="blue bold">代金券</span>';
                                    break;
                                    case '2':
                                    echo '<span class="red bold">现金券</span>';
                                    break;
                                    }
                                    ?></td>
                                <td>{$vo.add_time}</td>
                                <td>
                                    <a   style="{$update==1?'':'display: none'}" class="btn btn-xs btn-info radius5" href="{:U('Coupon/editcoupon')}/volume_id/{$vo.volume_id}"  style="margin-right: 5px;float:none;">
                                        <i class="ace-icon fa">编辑</i></a>
                                    <a class="btn btn-xs btn-info radius5 marginT10" href="{:U('Coupon/detail')}/volume_id/{$vo.volume_id}"/>
                                    <i class="ace-icon fa">详情</i></a>
                                </td>
                            </tr>
                        </volist>
                        <tr>
                            <td colspan="20" class="page">{$page}</td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </form>

</block>
<block name="javascript">
    <script>
        $('#send').click(function(){
            var s='';
            $('input[name=coupon]:checked').each(function(){
                s+=$(this).val()+',';
            });

            $('#tree').val(s);

            $.ajax({
                url:"admin.php?s=/Coupon/send",
                type:"post",
                data:{
                    id:s
                },
                success:function(msg){
                    console.log(msg)
                }
            });
        });


        //删除时选中的id值
        var checkedId = [];

        $(function(){
            $("#search").click(function () {
                var url = $(this).attr('url');
                var query = $('.search-form').find('input,select').serialize();
                query = query.replace(/(&|^)(\w*?\d*?\-*?_*?)*?=?((?=&)|(?=$))/g, '');
                query = query.replace(/^&/g, '');
                if (url.indexOf('?') > 0) {
                    url += '&' + query;
                } else {
                    url += '?' + query;
                }
                window.location.href = url;
            });



           $('#CheckAll').click(function (e) {
                $("input[name='coupon[]']").prop('checked', this.checked);
                if (e.currentTarget.checked) {
                    $("input[name='coupon[]']").each(function(i,elemenet){
                        var id = $(elemenet).attr("value");
                        checkedId.push(id);
                    })
                }else{
                    checkedId = [];
                }
                console.log(checkedId);
            });


            $("input[name='coupon[]']").click(function(e){
                console.log(e.currentTarget.checked);
                if (e.currentTarget.checked) {
                    var id = $(e.currentTarget).attr("value");
                    checkedId.push(id);
                    console.log(checkedId);
                }else{
                    checkedId.splice($.inArray(id,checkedId),1);
                    console.log(checkedId);
                }
            });


            $("#submitBtn").click(function(){
                var result = new Array(), str = '';
                $('input[name="coupon[]"]:checked').each(function () {
                    result.push($(this).val());
                });
                var str = result.join(',');
                if (str == '') {
                    $.jbox.info('没有选中任何数据！','提示');
                    return false;
                }

            });

            $("#Del").click(function(){
                var submit = function (v, h, f) {
                    if (v == true) {
                        var volume_id = checkedId;
                        if(volume_id.length == 0){
                            $.jbox.info('请选择需删除的数据','提示');
                        }else{
                            $.ajax({
                                url:"{:U('Coupon/del')}",
                                type:"post",
                                data:{
                                    Del: volume_id
                                },
                                success:function(data){
                                    if(data=='1'){
                                        $.jbox.tip("正在删除数据...", 'loading');
                                        // 模拟2秒后完成操作
                                        window.setTimeout(function () { $.jbox.tip('删除成功。', 'success');location.reload()}, 1000);
                                    }else if(data=='2'){
                                        $.jbox.info("删除失败!",'提示');
                                    }
                                }
                            });
                        }
                    }else{

                    }

                    return true;
                };
                // 自定义按钮
                $.jbox.confirm("确认删除吗？", "提示", submit, { buttons: { '确定': true, '取消': false} });

           });

        });
    </script>
</block>